package rai.util;

import java.io.InputStream;
import java.sql.DriverManager;
import java.util.Properties;
import org.apache.log4j.Logger;
/**
 * Connection prepare.
 * @author Tien
 *
 */
public class Connection {
    private static Properties props = new Properties();
    private static Logger log = Logger.getLogger("Connection");
    static {
        try {
            InputStream is = Connection.class
                    .getResourceAsStream("/db.properties");
            props.load(is);
        } catch (Exception ex) {
            // in case occur exception, log it.
            log.error("Load db.properties", ex);
        }
    }

    public static java.sql.Connection getConnection() {
        // load the driver and establish a connection.
        try {
            String jdbcDriver = props.getProperty(Constants.JDBD_DRIVER);
            String jdbcUrl = props.getProperty(Constants.JDBD_URL);
            String jdbcUser = props.getProperty(Constants.JDBD_USER);
            String jdbcPassword = props.getProperty(Constants.JDBD_PASS);

            Class.forName(jdbcDriver);
            return DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
        } catch (Exception ex) {
            // in case occur exception, log it.
            log.error("getConnection", ex);
        }
        return null;
    }
}
